//
//  PhotoAblumTableViewController.m
//  testApp
//
//  Created by iMac on 2018/2/8.
//  Copyright © 2018年 iMac. All rights reserved.
//

#import "PhotoAblumTableViewController.h"
#import "WDPhotoHandler.h"
#import "AblumModel.h"
#import "AssetModel.h"
#import "PhotoAblumCell.h"
#import "SelectPhotoController.h"



CGFloat cellHeight = 88;
static NSString *photoAblumCellID = @"photoAblumCellID";
@interface PhotoAblumTableViewController ()

@property (nonatomic, strong) NSArray *ablumModels;

@end

@implementation PhotoAblumTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadAblumModelsIsrefresh:false];
    
    [self addDismissButton];
}


#pragma mark - Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return cellHeight;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"%ld", self.ablumModels.count);
    return self.ablumModels.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PhotoAblumCell * cell = [tableView dequeueReusableCellWithIdentifier:photoAblumCellID];
    if (cell == nil) {
        cell = [[PhotoAblumCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:photoAblumCellID];
    }
    cell.model = self.ablumModels[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    AblumModel *model = self.ablumModels[indexPath.row];
    if (model.assetCount > 0) {
        SelectPhotoController *selectPhotoController = [[SelectPhotoController alloc] initWithCollectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
        selectPhotoController.collection = model.ablum;
        [self.navigationController pushViewController:selectPhotoController animated:true];
    }
}


- (void)loadAblumModelsIsrefresh:(BOOL)isRefresh {
    if (!isRefresh) {
        if (!self.ablumModels || self.ablumModels.count == 0) {
            self.ablumModels = [self loadAblum];
        }
    }
    self.ablumModels = [self loadAblum];
}
- (NSArray *)loadAblum {
    WDPhotoHandler *photoHandler = [WDPhotoHandler defaultHandler];
    NSArray<PHAssetCollection *> *ablums = [photoHandler collectionsFromSmartAlbums];
    NSMutableArray *mArr = [NSMutableArray array];
    for (NSInteger i = 0; i < ablums.count; i++) {
        PHAssetCollection *collection = ablums[i];
        
        PHAsset * asset = [photoHandler firstInCollection:collection];
        AblumModel * model = [AblumModel ablumModel:collection asset:asset assetCount:[photoHandler assetCountFromCollection:collection]];
        model.cellHeight = cellHeight;
        [mArr addObject:model];
    }
    return mArr.copy;
}


- (void)addDismissButton {
    UIButton *dismiss = ({
        UIButton *btn = [[UIButton alloc] init];
        [btn setTitle:@"dismiss" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        CGFloat f = 100;
        btn.frame = CGRectMake(SCREEN_WIDTH - f, SCREEN_HEIGHT - f, f, f);
        [btn setBackgroundColor:[UIColor purpleColor]];
        [btn addTarget:self action:@selector(dismissButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        btn;
    });
    [self.view addSubview:dismiss];
}
- (void)dismissButtonClick:(UIButton *)sender {
    [self dismissViewControllerAnimated:true completion:nil];
}
@end
